package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.CrmFollowUpVo;
import com.ruoyi.system.domain.CrmStudent;
import com.ruoyi.system.service.ICrmStudentService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CrmFollowUpMapper;
import com.ruoyi.system.domain.CrmFollowUp;
import com.ruoyi.system.service.ICrmFollowUpService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 跟进记录Service业务层处理
 * 
 * @author Bill
 * @date 2024-06-24
 */
@Service
public class CrmFollowUpServiceImpl implements ICrmFollowUpService 
{
    @Autowired
    private CrmFollowUpMapper crmFollowUpMapper;

    @Autowired
    private ICrmStudentService crmStudentService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询跟进记录
     * 
     * @param id 跟进记录主键
     * @return 跟进记录
     */
    @Override
    public CrmFollowUp selectCrmFollowUpById(Long id)
    {
        return crmFollowUpMapper.selectCrmFollowUpById(id);
    }

    @Override
    public CrmFollowUpVo selectCrmFollowUpVoById(Long id) {

        CrmFollowUpVo followUpVo = new CrmFollowUpVo();

        CrmFollowUp followUp = selectCrmFollowUpById(id);

        if(followUp != null) {
            BeanUtils.copyBeanProp(followUpVo, followUp);

            if(followUp.getFollowUpId() != null && followUp.getFollowUpId() != 0) {
                followUpVo.setFollowUpUser(userService.selectUserById(followUp.getFollowUpId()).getUserName());
            }
        }

        return followUpVo;
    }

    /**
     * 查询跟进记录列表
     * 
     * @param crmFollowUp 跟进记录
     * @return 跟进记录
     */
    @Override
    public List<CrmFollowUp> selectCrmFollowUpList(CrmFollowUp crmFollowUp)
    {
        return crmFollowUpMapper.selectCrmFollowUpList(crmFollowUp);
    }

    @Override
    public void selectCrmFollowUpVoList(TableDataInfo rspData) {
        List<CrmFollowUpVo> followUpVos = Collections.EMPTY_LIST;

        List<CrmFollowUp> followUps = (List<CrmFollowUp>) rspData.getRows();

        if(!CollectionUtils.isEmpty(followUps)) {
            followUpVos = new ArrayList<>(followUps.size());
            for (int i = 0; i < followUps.size() ; i++) {
                CrmFollowUpVo followUpVo = new CrmFollowUpVo();
                CrmFollowUp followUp = followUps.get(i);
                BeanUtils.copyBeanProp(followUpVo, followUp);
                if(followUp.getFollowUpId() != null && followUp.getFollowUpId() != 0) {
                    followUpVo.setFollowUpUser(userService.selectUserById(followUp.getFollowUpId()).getUserName());
                }

                followUpVos.add(followUpVo);
            }
        }

        rspData.setRows(followUpVos);
    }

    /**
     * 新增跟进记录
     * 
     * @param crmFollowUp 跟进记录
     * @return 结果
     */
    @Override
    public int insertCrmFollowUp(CrmFollowUp crmFollowUp)
    {
        crmFollowUp.setCreateTime(DateUtils.getNowDate());
        int i =crmFollowUpMapper.insertCrmFollowUp(crmFollowUp);

        updateStudentLatestFollowTime(crmFollowUp.getStudentId());

        return i;
    }

    /**
     * 修改跟进记录
     * 
     * @param crmFollowUp 跟进记录
     * @return 结果
     */
    @Override
    public int updateCrmFollowUp(CrmFollowUp crmFollowUp)
    {
        crmFollowUp.setUpdateTime(DateUtils.getNowDate());
        int i = crmFollowUpMapper.updateCrmFollowUp(crmFollowUp);


        crmFollowUp = selectCrmFollowUpById(crmFollowUp.getId());

        updateStudentLatestFollowTime(crmFollowUp.getStudentId());

        return i;
    }



    /**
     * 批量删除跟进记录
     * 
     * @param ids 需要删除的跟进记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmFollowUpByIds(String ids)
    {
        return crmFollowUpMapper.deleteCrmFollowUpByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除跟进记录信息
     * 
     * @param id 跟进记录主键
     * @return 结果
     */
    @Override
    public int deleteCrmFollowUpById(Long id)
    {
        return crmFollowUpMapper.deleteCrmFollowUpById(id);
    }

    @Override
    public CrmStudent selectCrmStudentById(Long id) {
        return crmStudentService.selectCrmStudentById(id);
    }


    /**
     * 更新学员最后跟进时间
     * @param studentId
     * @return
     */
    private int updateStudentLatestFollowTime(Long studentId) {
        CrmStudent oldStudent = selectCrmStudentById(studentId);
        CrmStudent student = new CrmStudent();
        student.setId(oldStudent.getId());
        student.setSalesmanId(oldStudent.getSalesmanId());
        student.setFollowTime(DateUtils.getNowDate());
        return crmStudentService.updateCrmStudent(student);
    }
}
